package de.dis2011.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class shop extends stadt
{
    private int shopid = -1;
    private String shopname;

    public int getShopid()
    {
        return shopid;
    }

    public void setShopid(int shopid)
    {
        this.shopid = shopid;
    }

    public String getShopname()
    {
        return shopname;
    }

    public void setShopname(String shopname)
    {
        this.shopname = shopname;
    }

    /**
     * Lädt eine Information aus der Datenbank
     * 
     * @param id
     * 
     * @return Informations-Instanz
     */
    public static shop load(int id)
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM shopid WHERE shopid = ?";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);
            pstmt.setInt(1, id);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
            {
                shop ts = new shop();
                ts.setShopid(rs.getInt("shopid"));
                ts.setStadtid(rs.getInt("stadtid"));
                ts.setShopname(rs.getString("name"));
                System.out.println("ShopID: " + ts.getShopid() + " StadtID: " + ts.getStadtid() + " Name: " + ts.getShopname());
            }
            rs.close();
            pstmt.close();

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static shop loadAll()
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM shopid";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                shop ts = new shop();
                ts.setShopid(rs.getInt("shopid"));
                ts.setStadtid(rs.getInt("stadtid"));
                ts.setShopname(rs.getString("name"));
                System.out.println("ShopID: " + ts.getShopid() + " StadtID: " + ts.getStadtid() + " Name: " + ts.getShopname());
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}
